package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.IntegralCommoditySize;
import com.ruoyi.recycle.dto.IntegralCommodityRepertory;
import com.ruoyi.recycle.dto.IntegralCommoditySizeDTO;
import com.ruoyi.recycle.service.IIntegralCommoditySizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分商品尺寸Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/size")
@Api(tags = "积分商品尺寸")
public class IntegralCommoditySizeController extends BaseController {
    @Autowired
    private IIntegralCommoditySizeService integralCommoditySizeService;

    /**
     * 查询积分商品尺寸列表
     */
    @RequiresPermissions("recycle:size:list")
    @GetMapping("/list")
    public TableDataInfo list(IntegralCommoditySize integralCommoditySize) {
        startPage();
        List<IntegralCommoditySize> list = integralCommoditySizeService.selectIntegralCommoditySizeList(integralCommoditySize);
        return getDataTable(list);
    }

    /**
     * 分页查询积分商品尺寸列表
     */
    @RequiresPermissions("recycle:size:list")
    @PostMapping("/sizePageQuery")
    @ApiOperation("分页查询积分商品尺寸列表")
    public IPage<IntegralCommoditySize> sizePageQuery(@RequestBody IntegralCommoditySizeDTO queryDTO) {
        final IntegralCommoditySize size = new IntegralCommoditySize();
        BeanUtils.copyProperties(queryDTO, size);
        final LambdaQueryWrapper<IntegralCommoditySize> wrapper = Wrappers.lambdaQuery(size);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(IntegralCommoditySize::getCreatedTime, queryDTO.getSearchTimeBegin());
        } else if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(IntegralCommoditySize::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.orderByDesc(IntegralCommoditySize::getCreatedTime);
        return integralCommoditySizeService.page(new Page<IntegralCommoditySize>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出积分商品尺寸列表
     */
    @RequiresPermissions("recycle:size:export")
    @Log(title = "积分商品尺寸", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出积分商品尺寸列表")
    public void export(HttpServletResponse response, IntegralCommoditySize integralCommoditySize) {
        List<IntegralCommoditySize> list = integralCommoditySizeService.selectIntegralCommoditySizeList(integralCommoditySize);
        ExcelUtil<IntegralCommoditySize> util = new ExcelUtil<IntegralCommoditySize>(IntegralCommoditySize.class);
        util.exportExcel(response, list, "积分商品尺寸数据");
    }

    /**
     * 获取积分商品尺寸详细信息
     */
    @RequiresPermissions("recycle:size:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取积分商品尺寸详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(integralCommoditySizeService.selectIntegralCommoditySizeById(id));
    }

    /**
     * 新增积分商品尺寸
     */
    @RequiresPermissions("recycle:size:add")
    @Log(title = "积分商品尺寸", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增积分商品尺寸")
    public AjaxResult add(@RequestBody IntegralCommoditySize integralCommoditySize) {
        return toAjax(integralCommoditySizeService.insertIntegralCommoditySize(integralCommoditySize));
    }

    /**
     * 修改积分商品尺寸
     */
    @RequiresPermissions("recycle:size:edit")
    @Log(title = "积分商品尺寸", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改积分商品尺寸")
    public AjaxResult edit(@RequestBody IntegralCommoditySize integralCommoditySize) {
        return toAjax(integralCommoditySizeService.updateIntegralCommoditySize(integralCommoditySize));
    }

    /**
     * 删除积分商品尺寸
     */
    @RequiresPermissions("recycle:size:remove")
    @Log(title = "积分商品尺寸", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除积分商品尺寸")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(integralCommoditySizeService.deleteIntegralCommoditySizeByIds(ids));
    }

    /**
     * 商品库存管理
     */
    @PostMapping("/integralCommodityRepertory")
    @ApiOperation("商品库存管理")
    public TableDataInfo integralCommodityRepertory(@RequestParam String sizeName) {
        startPage();
        List<IntegralCommodityRepertory> list = integralCommoditySizeService.integralCommodityRepertory(sizeName);
        return getDataTable(list);
    }

}
